package com.roubsite.cloud.security.controller;

import com.roubsite.cloud.common.bean.Result;
import com.roubsite.cloud.common.bean.enums.CommonStatusCodeEnum;
import com.roubsite.cloud.common.bean.enums.UserStatusCodeEnum;
import com.roubsite.cloud.common.userinfo.SecurityUserDetails;
import com.roubsite.cloud.common.utils.SpringUtil;
import com.roubsite.cloud.security.bean.LoginUserEntity;
import com.roubsite.cloud.security.bean.RcUsers;
import com.roubsite.cloud.security.bean.TokenEntity;
import com.roubsite.cloud.security.service.UserDetailsServiceImpl;
import com.roubsite.cloud.security.utils.TokenUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class LoginController {
	@Autowired
	SpringUtil springUtil;
	@Autowired
	UserDetailsServiceImpl userDetailsService;
	@Autowired
	TokenUtil tokenUtil;
	private static final BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

	@PostMapping("/login")
	public Result login(@RequestBody LoginUserEntity userBean) {
		//校验验证码是否正确
		String captchaKey = userBean.getCaptchaKey();
		String captchaCode = userBean.getCaptchaCode();
		if (!springUtil.verifyCode(captchaCode, captchaKey)) {
			//验证码错误
			return Result.setResultStatusCode(CommonStatusCodeEnum.VERIFY_CODE_ERROR);
		}
		//验证码验证通过，进行用户信息的认证
		RcUsers rcUsers = userDetailsService.getUser(userBean.getUsername());
		if (ObjectUtils.isEmpty(rcUsers)) {
			return Result.setResultStatusCode(UserStatusCodeEnum.ACCOUNT_NOT_EXIST);
		}
		//验证密码是否正确
		if (!bCryptPasswordEncoder.matches(userBean.getPassword(), rcUsers.getUserPassword())) {
			return Result.setResultStatusCode(UserStatusCodeEnum.LOGIN_PASSWORD_ERROR);
		}
		//验证用户状态
		if (!"1".equals(rcUsers.getUserStatus())) {
			return Result.setResultStatusCode(UserStatusCodeEnum.ACCOUNT_DISABLE);
		}
		//所有验证都通过了，生成token返回token
		List<String> accessURLs = userDetailsService.getUserAccessUrlsByUid(rcUsers.getUserId());
		SecurityUserDetails securityUserDetails = new SecurityUserDetails();
		securityUserDetails.setUserName(rcUsers.getUserName());
		securityUserDetails.setPassword(rcUsers.getUserPassword());
		securityUserDetails.setUserId(rcUsers.getUserId());
		securityUserDetails.setAccessURLs(accessURLs);
		String refToken = tokenUtil.generateRefreshToken(securityUserDetails, rcUsers.getUserId());
		String token = tokenUtil.generateToken(securityUserDetails, rcUsers.getUserId());
		TokenEntity tokenEntity = new TokenEntity();
		tokenEntity.setAccessToken(token);
		tokenEntity.setRefreshToken(refToken);
		tokenEntity.setTokenType("Bearer");
		tokenEntity.setExpires(tokenUtil.tokenExpired + (System.currentTimeMillis() / 1000));
		return new Result(tokenEntity);
	}


}
